package com.example.cwproject.service.serviceImpl;

import com.example.cwproject.entity.Picture;
import com.example.cwproject.entity.Post;
import com.example.cwproject.mapper.PictureMapper;
import com.example.cwproject.mapper.PostMapper;
import com.example.cwproject.service.PostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class PostServiceImpl implements PostService {

    @Autowired
    private PostMapper postMapper;

    @Autowired
    private PictureMapper pictureMapper;

    @Override
    public Integer addPost(Post post) {
        Date date = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
        String time = formatter.format(date);
        postMapper.insert(post.getUserID(),post.getText(), post.getTitle(), post.getRoad(),post.getDuration(),time);
        Integer postID = postMapper.selectPostID(post.getUserID(),time);
        return postID;
    }

    @Override
    public void delete(Integer postID) {

        postMapper.delete(postID);
        pictureMapper.delete(postID);
    }

    @Override
    public List<Post> selectAllPost() {
        List<Post> allpost = postMapper.selectAllPost();

        for(Post npost:allpost){
            String pictureImage;
            if(postMapper.findPictureImage(npost.getPostID()) != null){
                pictureImage = postMapper.findPictureImage(npost.getPostID());
            }else{
                pictureImage = null;
            }
            npost.setPostImage(pictureImage);
            System.out.println(npost);
        }

        return allpost;
    }

    @Override
    public  List<Post> selectByEmail(String email){
        List<Post> allpost = postMapper.selectByEmail(email);

        for(Post npost:allpost){
            String pictureImage;
            if(postMapper.findPictureImage(npost.getPostID()) != null){
                pictureImage = postMapper.findPictureImage(npost.getPostID());
            }else{
                pictureImage = null;
            }
            npost.setPostImage(pictureImage);
            System.out.println(npost);
        }

        return allpost;
    }
    @Override
    public List<Post> selectByUser(String userName) {
        List<Post> allpost = postMapper.selectByUser(userName);

        for(Post npost:allpost){
            String pictureImage;
            if(postMapper.findPictureImage(npost.getPostID()) != null){
                pictureImage = postMapper.findPictureImage(npost.getPostID());
            }else{
                pictureImage = null;
            }
            npost.setPostImage(pictureImage);
            System.out.println(npost);
        }

        return allpost;

    }


    @Override
    public String uploadImage(String url, Integer postID){

        Picture dbpicture = pictureMapper.select(postID);
        if(dbpicture == null){
            pictureMapper.insert(postID,url);
        }else{
            String imagUrl = pictureMapper.selectUrl(postID);
            imagUrl = imagUrl +"|"+url;
            pictureMapper.updateUrl(imagUrl,postID);
        }
        return pictureMapper.selectUrl(postID);

    }
}
